// pages/allotPro/allotPro5/allotPro5.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    pageSize: 25,
    list: [],
    selectOn: 0,
    selectOnuserId: "",
    selectOnagencyCode: "",
    searchValue: "",
    agencyCode: "", // 分支行id
    clientid: "", // old 客户id,
    result: [],
    corpType: '',
  },
  onSearch (e) {
    this.setData({
      searchValue: e.detail,
      pageNum: 1,
      list: []
    })
    this.getList()
  },
  onChange (e) {
    console.log(e);
    this.setData({
      selectOn: e.detail,
    })
  },
  commitClick () {
    if (this.data.selectOn !== "") {
      let paramArr = []
      this.data.result.forEach((item, idnex) => {
        let param = {
          userId: this.data.clientid,
          corpCredit: item,
          allocUserId: this.data.list[this.data.selectOn].userId,
          agencyCode: this.data.list[this.data.selectOn].agencyCode
        }
        paramArr.push(param)
      })
      console.log(paramArr)
      getApp().globalData.request(
        "/visit/allocate", paramArr,
        "POST"
      ).then(res => {
        console.log(res);
        if (res.code == 200) {
          if (this.data.corpType == '1') {
            wx.navigateTo({
              url: '/pages/undistributed3/undistributed3',
            })
          } else {
            wx.navigateTo({
              url: '/pages/undistributed3/undistributed3',
            })
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none"
          })
        }
      })
    } else {
      wx.showToast({
        title: '请先选择！',
        icon: "none"
      })
    }
  },
  getList () {
    console.log({
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    });
    getApp().globalData.request(
      "/visit/show/manager_list", {
      searchParam: this.data.searchValue,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
    },
      "POST"
    ).then(res => {
      console.log(res);
      if (res.data.list.length > 0) {
        this.setData({
          list: this.data.list.concat(res.data.list)
        })
      }
    })
  },
  // 加载更多
  scrollBottom () {
    this.data.pageNum += 1
    this.setData({
      pageNum: this.data.pageNum
    })
    this.getList()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    var arr = options.result.split(",")
    console.log(arr);
    this.setData({
      agencyCode: options.agencyCode,
      clientid: options.clientid,
      result: arr,
      pageNum: 1,
      list: [],
      corpType: options.corpType
    })
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () {

  }
})